package com.lby.redisdemo.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lby.redisdemo.bean.SimpleBean;
import com.lby.redisdemo.constant.SimpleMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import sun.java2d.pipe.SpanShapeRenderer;

/**
 * 消费者
 *
 * @ClassName SimpleConsumer
 * @Description TODO
 * @Author louby0923
 * @Date 2023/5/30 11:38
 * @Version 1.0
 */
@Slf4j
//@Component
public class SimpleConsumer {

    @Autowired
    private MongoTemplate mongoTemplate;

    @RabbitListener(queuesToDeclare = @Queue(SimpleMqConstant.HANDLER_OBJECT_QUEUE_NAME))
    @RabbitHandler
    public void receiveObject(SimpleBean simple) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String message = objectMapper.writeValueAsString(simple);
        log.info("simple consumer receive the object:{}", message);

        System.out.println(message);
        mongoTemplate.insert(simple);
    }
}
